package cn.gd.service.systemAdministration.User.Impl;

import cn.gd.mapper.systemAdministration.User.UserMapper;
import cn.gd.pojo.Organizationstructure;
import cn.gd.pojo.Roles;
import cn.gd.pojo.Users;
import cn.gd.service.systemAdministration.User.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public PageInfo<Users> queryUsers(Integer pageNum, Integer pageSize, Users users) {
        PageHelper.startPage(pageNum, pageSize);
        List<Users> list = mapper.queryUsers(users);
        return new PageInfo<>(list);
    }

    @Override
    public Boolean deleteUserByUserId(Integer userId) {
        boolean flag = false;
        int row = mapper.deleteUserByUserId(userId);
        if (row > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public Users queryUserByUserId(Integer userId) {
        return mapper.queryUserByUserId(userId);
    }

    @Override
    public List<Roles> queryRole() {
        return mapper.queryRole();
    }

    @Override
    public List<Organizationstructure> queryOrganization() {
        return mapper.queryOrganization();
    }

    @Override
    public Boolean updateUserByUserID(Users users) {
        boolean flag = false;
        int row = mapper.updateUserByUserID(users);
        if(row > 0) {
            flag = true;
        }
        return flag;
    }

    @Transactional
    @Override
    public Boolean addUser(Users users) {
        boolean flag = false;
        int row = mapper.addUser(users);
        if(row > 0) {
            // 使用生成的用户ID插入用户角色关联信息
            mapper.addUserRoles(users.getUserID(), users.getRoles().getRoleID());
            // 使用生成的用户ID插入用户部门关联信息
            mapper.addUserDepartment(users.getUserID(), users.getOrganizationStructure().getDepartmentss().getDeptID());
            flag = true;
        }
        return flag;
    }
}
